Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
My simple "send message" javascript program for Twilio is looping twice, but its only sending a message to the same number, What's missing?

I'm trying to create a simple Javascript program that will send a single message to an array of different numbers. I found a twilio page that got me almost there but it doesn't tell me how to loop. I'm admittedly a programming nubie and found a line of loop code. The program did loop, but it called the same number in the array twice instead of calling the two numbers in the array. Here is the code I was using:

// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const twilio = require('twilio')(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);
const numbers = [+180xxxxxxxx, +180xxxxxxxx,]
const body = 'Hello World'
let nLen = numbers.length
for (let i = 0; i<nLen; i++)
{
twilio.messages
  .create({
        to: numbers,
        from: +180xxxxxxxx,
        body: body
      })
  .then(message => {
        console.log(message.sid);
  })
  .catch(err => console.error(err));
}

Is there another function I need to add in to make it send the message to the second number the second time?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I've never used twilio but it looks like you give the array of numbers in the .create function instead of one number,

try switching "numbers" to "numbers[i]":

for (let i = 0; i<nLen; i++)
{
twilio.messages
  .create({
        to: numbers[i],
        from: +180xxxxxxxx,
        body: body
      })
  .then(message => {
        console.log(message.sid);
  })
  .catch(err => console.error(err));
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda